package com.course.finalproject.model;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * Class that represent a battery log.
 * @author	Mariano Bessone <mariano.bessone @ tallertechnologies.com>
 * @since	2012-09-18
 */
public class BatteryLog implements Parcelable {

	/** Attributes. */
	private String date;
	private String time;
	private int level;
	private int scale;
	
	/**
	 * Constructor.
	 * 
	 * @param date The date of this log.
	 * @param time The time in hh:mm:ss of this log.
	 * @param level The battery level.
	 * @param scale The maximum level possible.
	 */
	public BatteryLog(String date, String time, int level, int scale) {
		this.date = date;
		this.time = time;
		this.level = level;
		this.scale = scale;
	}
	
	/**
	 * Constructor from a parcel.
	 * 
	 * @param in The parcel object to reconstruct a battery log object.
	 */
	public BatteryLog(Parcel in) {
		this.date = in.readString();
		this.time = in.readString();
		this.level = in.readInt();
		this.scale = in.readInt();
	}

	/**
	 * Gets the date of this log.
	 * 
	 * @return The date of this log.
	 */
	public String getDate() {
		return date;
	}
	
	/**
	 * Gets the time of this log.
	 * 
	 * @return The time of this log.
	 */
	public String getTime() {
		return time;
	}
	
	/**
	 * Gets the battery level of this log.
	 * 
	 * @return The battery level of this log.
	 */
	public int getLevel() {
		return level;
	}
	
	/**
	 * Gets the scale: the maximum battery level possible.
	 * 
	 * @return The maximum battery level possible.
	 */
	public int getScale() {
		return scale;
	}
	
	/**
	 * Gets the percentage of remain battery.
	 * 
	 * @return The percentage of remain battery.
	 */
	public String getBatteryPercentage() {
		int percentage = 0;
		if (level >= 0 && scale > 0) {
			percentage = (level * 100) / scale;
        }
		return String.valueOf(percentage);
	}
	
	/**
	 * Returns the string representation of this log.
	 * 
	 * @return The string representation of this log.
	 */
	public String toString() {
		return date + " " + time + " - Battery: " + getBatteryPercentage() + "%";
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(date);
		dest.writeString(time);
		dest.writeInt(level);
		dest.writeInt(scale);
	}
	
	/**
	 * BatteryLog parcelable creator.
	 */
	public static final Parcelable.Creator<BatteryLog> CREATOR = new Parcelable.Creator<BatteryLog>() {
		public BatteryLog createFromParcel(Parcel in) {
		    return new BatteryLog(in);
		}
		
		public BatteryLog[] newArray(int size) {
		    return new BatteryLog[size];
		}
	};

}
